﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameEngine.PositionConverter
{
    /// <summary>
    /// International checkers position converter
    /// </summary>
    public class InternationalPositionConverter: BasePositionConverter
    {
        private const int lMinPosition = 1;
        private const int lMaxPosition = 50;
        private const int _boardSize = 10;

        public override int BoardSize
        {
            get
            {
                return _boardSize;
            }
        }

        /// <summary>
        /// Parses International Position of checkers 
        /// </summary>
        public override bool TryParse(string sPosition, out Position pos)
        {
            pos = Position.Empty;

            // first check number
            int lPosition = 0;
            if (int.TryParse(sPosition, out lPosition) == false)
                return false;

            // check position validity
            if (lPosition < lMinPosition || lPosition > lMaxPosition)
                return false;

            // convert to row and col
            int row = 9 - ((lPosition - 1) / 5);
            int colTemp = (lPosition - 1) % 5;
            int col = 0;
            if (row % 2 == 0)
            {
                col = colTemp * 2;
            }
            else
            {
                col = colTemp * 2 + 1;
            }


            pos = new Position(row, col);
            return true;
        }

        /// <summary>
        /// Converts the field to the check number of notation
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        private string PositionToNumberNotation(Position pos)
        {
            int colIndex = pos.Col;
            if (pos.Row % 2 != 0)
                colIndex--;
            colIndex /= 2;

            int field = ((9 - pos.Row) * 5 + colIndex) + 1;

            return field.ToString();
        }

        /// <summary>
        /// Converts to the number notation given field
        /// </summary>
        public override string PositionToString(Position pos, bool bChess)
        {
            if (bChess == true)
                throw new NotImplementedException();
            else
                return this.PositionToNumberNotation(pos);
        }
    }
}
